/* 
 * Song Exporter
 *
 * Choi Jonghwan<saxboy@saxboy.pe.kr>
 * 2006/04/23
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "songexporter.h"

extern int song_exporter_handler_midi(SONG song); 
extern int song_exporter_handler_abc(SONG song); 
extern int song_exporter_handler_text(SONG song); 

struct __exporter_binding {
	char *type; 
	int (*handler)(SONG song); 
}; 

struct __exporter_binding g_exporter_list[] = {
	{ "MIDI", song_exporter_handler_midi }, 
	{ "ABC", song_exporter_handler_abc }, 
	{ "TEXT", song_exporter_handler_text }, 
	{ NULL, NULL}
}; 


SONGEXPORTER songexporter_New(void)
{
	SONGEXPORTER se=NULL; 
	se = (SONGEXPORTER)malloc(sizeof(_SONGEXPORTER)); 
	if (se!=NULL)
	{
		memset(se, 0, sizeof(_SONGEXPORTER)); 
	}
	return se; 
}

SONGEXPORTER songexporter_NewWithType(const char *type)
{
	SONGEXPORTER se=songexporter_New(); 
	if (se!=NULL)
	{
		if ( songexporter_Load(se, type) > 0 )
			return se; 
	}

	songexporter_Delete(se); 
	return NULL; 
}

int songexporter_Load(SONGEXPORTER se, const char *type)
{
	if (se==NULL) return -1; 
	if (type==NULL) return -1; 

	/* find proper handler */
	{
		int i=0; 

		for (i=0; g_exporter_list[i].type!=NULL; i++)
		{
			if ( strcmp(type, g_exporter_list[i].type ) == 0 )
			{
				se->handler = g_exporter_list[i].handler; 
				return 1; 
			}
		}
	}

	return -1; 
}

int songexporter_Delete(SONGEXPORTER se)
{
	if (se==NULL) return -1; 
	free(se); 
	return 1; 
}

int songexporter_SetSong(SONGEXPORTER se, SONG song)
{
	if (se==NULL) return -1; 
	if (song==NULL) return -1; 

	se->song = song; 
	return 1; 
}

int songexporter_Run(SONGEXPORTER se)
{
	if (se==NULL) return -1; 
	if (se->handler==NULL) return -1; 
	return se->handler(se->song); 
}



